## Figure 1: Hate Crime Trends

rm(list = ls())

setwd('/path/to/replication/')

library(data.table)
library(ggplot2)

crimes <- fread('./data/2007_2020_hate_crimes_classified.csv')

crimes[region_broad!='',label:='region of origin']
crimes[immigrant!='' & region_broad=='', label:='immigrant']
crimes[(religion_broad!='' | fascism!='') & (region_broad=='' & region_broad==''), label:='religion']
crimes[is.na(label), label:='other']

crimes[,date:=as.Date(crimes$Date, '%d-%m-%Y')]
crimes[,month:=as.Date(cut(date,breaks = "month", start.on.monday = FALSE))]

crimes_all <- crimes[label!='other', .N, by=month]
setnames(crimes_all,'N','All')
crimes_region <- crimes[region_broad!='',.N,by=month]
setnames(crimes_region,'N','region_classify')
crimes_all <- merge(crimes_all,crimes_region,by='month',all.x = T)
crimes_asian <- crimes[region_broad=='Asia', .N, by=month]
crimes_all <- merge(crimes_all,crimes_asian,by='month',all.x = T)
setnames(crimes_all,'N','Asian')
crimes_all[(!is.na(region_classify) & is.na(Asian)),Asian:=0]
crimes_all[,not_asian:=region_classify-Asian]
crimes_african <- crimes[region_broad=='Africa', .N, by=month]
crimes_all <- merge(crimes_all,crimes_african,by='month',all.x = T)
setnames(crimes_all,'N','African')
crimes_all[(!is.na(region_classify) & is.na(African)),African:=0]

crimes_plot <- melt(crimes_all, id.vars = 'month', 
                    measure.vars = c('All', 'region_classify', 'Asian', 'African', 'not_asian'))
crimes_plot[variable=='not_asian', variable:='not asian']
crimes_plot$variable <- factor(crimes_plot$variable, levels = c('Asian', 'African', 'All','not asian','region_classify'))

crimes_plot[,label:=variable]
crimes_plot[,label:=ifelse(month=="2018-01-01",as.character(label),NA)]

## Figure 1 (a)
ggplot(crimes_plot[(variable=='Asian' | variable=='African' | variable=='All')], aes(month, value,  group = variable)) +
  geom_line(aes(color = variable), size = 0.5) +
  geom_label(aes(label = label), nudge_x = 0.35, size = 4) +
  scale_colour_grey(start = 0.2, end = 0.8) +
  scale_x_date(breaks = "6 month", minor_breaks = "1 month", date_labels = '%Y-%m',
               limits = c(min(crimes_plot$month), max = as.Date('03-01-2021', format='%m-%d-%Y')),
               expand=c(0,0)) + xlab(NULL) +
  ylab("Hate crimes") +
  geom_vline(xintercept = as.numeric(as.Date('01-01-2020','%d-%m-%Y')), color='blue', linetype='dashed', size=0.2) +
  geom_text(x=as.numeric(as.Date('04-01-2019','%d-%m-%Y')), y=180, label="Covid-19", color='blue') +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position ='none',
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"
        ),axis.title.y = element_text(size = rel(1.2)), axis.title.x = element_text(size = rel(1.1)))
ggsave('./crime_trends_all_2020_months.pdf', width=6, height=4.5)

crimes_plot_z <- crimes_plot[month>='2018-01-01']
crimes_plot_z[,label:=variable]
crimes_plot_z[,label:=ifelse(month=="2019-01-01",as.character(label),NA)]

## Figure 1 (b)
ggplot(crimes_plot_z[(variable=='Asian' | variable=='African')], aes(month, value,  group = variable)) +
  geom_line(aes(color = variable), size = 0.5) +
  geom_label(aes(label = label), nudge_x = 0.35, size = 4) +
  scale_colour_grey(start = 0.2, end = 0.6) +
  scale_x_date(breaks = "6 month", minor_breaks = "1 month", date_labels = '%Y-%m',
               limits = c(min(crimes_plot_z$month), max = as.Date('03-01-2021', format='%m-%d-%Y')),
               expand=c(0,0)) + xlab(NULL) +
  ylab("Hate crimes") +
  geom_vline(xintercept = as.numeric(as.Date('01-01-2020','%d-%m-%Y')), color='blue', linetype='dashed', size=0.2) +
  geom_text(x=as.numeric(as.Date('10-01-2019','%m-%d-%Y')), y=40, label="Covid-19", color='blue') +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        legend.position ='none',
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"
        ),axis.title.y = element_text(size = rel(1.2)), axis.title.x = element_text(size = rel(1.1)))
ggsave('./crime_trends_zoom_all_2020_months_asian_african.pdf', width=6, height=4.5)